'use strict';
var http = require('http')
var fs = require('fs')
var cheerio = require('cheerio')
var superagent = require('superagent');
require('superagent-charset')(superagent)  // 用来设置编码

// var fastXmlParser = require('fast-xml-parser');
var dom = require("xmldom").DOMParser
var url = 'http://bet.hkjc.com/marksix/Fixtures.aspx?lang=CH';//初始url

// superagent.get(url)
// .charset('utf-8')
// .withCredentials()
// .end(function(err,res){
//   var a = res.header
//   var html = res.text;
//   debugger
// })

// superagent.get(url)
// .charset('utf-8')
// .set('Cookie','BotMitigationCookie_9518109003995423458="405641001555996146VNsr3G9qTp6V+to9zKRlH4ZDBlk="')
// .end(function(err,res){
//     var html = res.text;
//     var $ = cheerio.load(html)
//     var script = $('html').find('script');
//     console.log(html)
//     script.each(function(i,ele) {
//       var scripthtml = $(this).html();
//       if(scripthtml.match('xmldocobject')){

//         var doc = new dom().parseFromString(scripthtml,'text/xml')
//         // console.log(doc.documentElement.getElementsByTagName('drawMonth').length);
        
//         var drawYear = doc.documentElement.getElementsByTagName('drawYear');
//         var year = []
//         for(var n=0; n<drawYear.length ; n++){
//           var drawMonth =drawYear[n].getElementsByTagName('drawMonth')
//           for(var i=0; i<drawMonth.length; i++){
//             var day = drawMonth[i].getElementsByTagName('drawDate');
//             var dayArr = []
//             for(var j=0; j<day.length; j++){
//               if(day[j].getAttribute('draw') == "1" || day[j].getAttribute('jackpot') == "1"){
//                 dayArr.push({
//                   data:day[j].getAttribute('date'),
//                   draw:day[j].getAttribute('draw')
//                 })
//               }
//             }
//             year.push({year:drawYear[n].getAttribute('year'),month:drawMonth[i].getAttribute('month'),day:dayArr})
//           }
//         }
//         debugger
//         console.log(year);
//         qishu(year)
        
//         // savedContent($,scripthtml)
//       }
//     })
// });

function  qishu(year) {
  var  firstqishu19 = 2019001   // 18年第一次期数
  var  firstqishu18 = 2018073  // 17年7月份的第一次期数
  var  body = [] // 要写进的主体
  year.map(function (item,i) {
    console.log(item);
    var qishu = ""
    
    item.day.map(function (e,index) {
      if(item.year == '2019'){
        qishu = firstqishu19 ++
      }else if(item.year == '2018'){
        qishu = firstqishu18++
      }
      body.push({
        itemCode:"HK6",
        qishu: qishu,
        data: item.year + '-' + doubleNum(item.month) + '-' + doubleNum(e.data)
      })

    })
  })
  // console.log(body);
  savedContent(body)
}



function savedContent(new_art) {
  var body = ""
  new_art.map(function (item,i) {
    body += `${item.itemCode},${item.qishu},${item.qishu},${item.data} 21:30:00,${item.data} 09:30:00\r\n`
  })
  // var body = JSON.stringify(new_art);
  fs.writeFile('./data/'+'hk62'+'.txt',body,'utf-8',function(err){
      if(err){
          console.log(err)
      }
      
  })
}

function doubleNum(str){
    if(str<10){
      return "0"+str
    }else{
      return str
    }
}